/**
 * Copyright：2020，TLF工作室。
 */
package com.cqeec.netdisc.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cqeec.netdisc.model.User;
import com.cqeec.netdisc.service.FileService;

/**
 * 文件控制器。
 * 
 * @author 唐礼飞
 * 
 * @date 2020-11-03 15:54:27
 *
 */
@RestController
public class FileController {

	@Autowired
	private FileService fileService;
	
	@PostMapping("/batch/delete/file")
	public Map<String, Object> batchDelete(String names, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		if (obj == null) {
			// 获取当前用户
			map.put("code", 1);
			map.put("message", "用户信息丢失！");
		} else {
			User user = (User) obj;
			String[] name = names.split(",");
			for (String s : name) {
				fileService.deleteFile(user.getLoginName(), s);
			}
			map.put("code", 0);
			map.put("message", "删除文件成功！");
		}
		return map;
	}

	@PostMapping("/delete/file")
	public Map<String, Object> delete(String name, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		if (obj == null) {
			// 获取当前用户
			map.put("code", 1);
			map.put("message", "用户信息丢失！");
		} else {
			User user = (User) obj;
			fileService.deleteFile(user.getLoginName(), name);
			map.put("code", 0);
			map.put("message", "删除文件成功！");
		}
		return map;
	}
}
